﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;

namespace TestEngine
{
    public class Animation
    {
        int frame = 0;
        int frameX = 0;
        int frameY = 0;
        public Texture2D ani_Text;
        int speed = 0;
        int timer = 0;
        int frameTotal = 0; //Total Count of frames
        int frameCountX;
        int frameCountY;
        
      public Animation(Texture2D _text, int framesX, int framesY, int _framesTotal, int _speed)
        {
            ani_Text = _text;
            frameCountX = framesX;
            frameCountY = framesY;
            frameTotal = _framesTotal;
            speed = _speed;           
        }

       public void update(int d_time)
        {
            timer += speed * d_time;

            if (timer >= 60)
            {
                timer = 0;

                frame++; //Framing like a boss!

                frameX++; //Counts per column in line

                if (frameX >= frameCountX)
                {
                    frameX = 0;
                    frameY++; //Switches line
                    if (frameY >= frameCountY)
                    {
                        frameY = 0;
                    }
                }

                //This is just in case the total frames in use are lower than the framecount
                if (frame >= frameTotal)
                {
                    frame = 0;
                    frameX = 0;
                    frameY = 0;
                }
            }
        }

        public Rectangle getRect()
        {
            int w = ani_Text.Width / frameCountX;
            int h = ani_Text.Height / frameCountY;

            int x = (frameX%frameCountX)*w;
            int y = (frameY%frameCountY)*h;

            return new Rectangle(x, y, w, h);
        }
    }
}
